# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
pkgname=nextcloud
pkgver=19.0.1
pkgrel=0
pkgdesc="A safe home for all your data"
url="http://nextcloud.com"
arch="noarch"
license="AGPL-3.0-only"
_php=php7
_php_mods="-bcmath -ctype -curl -dom -gd -fileinfo -gmp -iconv -intl -json
	-mbstring -openssl -pcntl -posix -session -simplexml -xml -xmlreader
	-xmlwriter -zip"
depends="ca-certificates $_php ${_php_mods//-/$_php-}"
makedepends="xmlstarlet"
provides="$pkgname-contactsinteraction=$pkgver-r$pkgrel
	$pkgname-dav=$pkgver-r$pkgrel
	$pkgname-files=$pkgver-r$pkgrel
	$pkgname-federatedfilesharing=$pkgver-r$pkgrel
	$pkgname-lookup_server_connector=$pkgver-r$pkgrel
	$pkgname-oauth2=$pkgver-r$pkgrel
	$pkgname-provisioning_api=$pkgver-r$pkgrel
	$pkgname-settings=$pkgver-r$pkgrel
	$pkgname-twofactor_backupcodes=$pkgver-r$pkgrel
	$pkgname-workflowengine=$pkgver-r$pkgrel
	"
install="$pkgname.pre-install $pkgname.pre-upgrade $pkgname.post-upgrade
	$pkgname-initscript.post-install"
subpackages="$pkgname-doc $pkgname-initscript $pkgname-mysql $pkgname-pgsql $pkgname-sqlite
	$pkgname-default-apps:_default_apps"
source="https://download.nextcloud.com/server/releases/nextcloud-$pkgver.zip
	nextcloud19-dont-chmod.patch
	nextcloud17-app-encryption-info-add-mcrypt.patch
	dont-update-htaccess.patch
	disable-integrity-check-as-default.patch
	iconv-ascii-translit-not-supported.patch
	use-external-docs-if-local-not-avail.patch

	$pkgname-config.php
	$pkgname.logrotate
	$pkgname.confd
	$pkgname.cron
	fpm-pool.conf
	occ
	"
options="!check"
pkgusers="nextcloud"
pkggroups="www-data"
builddir="$srcdir"/$pkgname

# List of bundled apps to separate into subpackages. Keep it in sync!
# Note: Don't add "contactsinteraction", "files", "dav", 
# "federatedfilesharing", "lookup_server_connector", "provisioning_api",
# "oauth2", "settings", "twofactor_backupcodes", "workflowengine"
# here, these should be always installed.
_apps="accessibility
	activity
	admin_audit
	cloud_federation_api
	comments
	encryption
	federation
	files_external
	files_pdfviewer
	files_rightclick
	files_sharing
	files_trashbin
	files_versions
	files_videoplayer
	firstrunwizard
	logreader
	nextcloud_announcements
	notifications
	password_policy
	photos
	privacy
	recommendations
	serverinfo
	support
	sharebymail
	survey_client
	systemtags
	text
	theming
	user_ldap
	viewer
	"
for _i in $_apps; do
	subpackages="$subpackages $pkgname-$_i:_package_app"
done

# Directory for apps shipped with Nextcloud.
_appsdir="usr/share/webapps/$pkgname/apps"

package() {
	local basedir="var/lib/$pkgname"
	local datadir="$basedir/data"
	local wwwdir="usr/share/webapps/$pkgname"
	local confdir="etc/$pkgname"

	mkdir -p "$pkgdir"
	cd "$pkgdir"

	mkdir -p ./${wwwdir%/*}
	cp -a "$builddir" ./$wwwdir

	chmod +x ./$wwwdir/occ
	chmod 664 ./$wwwdir/.htaccess \
		./$wwwdir/.user.ini

	# Let's not ship upstream's 'updatenotification' app and updater, which
	# has zero chance of working and a big chance of blowing things up.
	rm -r ./$wwwdir/apps/updatenotification \
		./$wwwdir/lib/private/Updater

	# Replace bundled CA bundle with ours.
	ln -sf /etc/ssl/certs/ca-certificates.crt ./$wwwdir/resources/config/ca-bundle.crt

	install -d -m 770 -o nextcloud -g www-data \
		./$confdir ./$datadir ./$basedir/apps
	install -d -m 775 -o nextcloud -g www-data \
		./var/log/$pkgname

	# Create symlink from web root to site-apps, so web server can find
	# assets w/o explicit configuration for this layout.
	ln -s /$basedir/apps ./$wwwdir/apps-appstore

	mv ./$wwwdir/config/* ./$confdir/
	rm -r ./$wwwdir/config
	ln -s /$confdir ./$wwwdir/config

	mkdir -p ./usr/share/doc/$pkgname
	mv ./$wwwdir/core/doc ./usr/share/doc/$pkgname/core

	install -m 660 -o nextcloud -g www-data \
		"$srcdir"/$pkgname-config.php ./$confdir/config.php

	install -m 644 -D "$srcdir"/$pkgname.logrotate ./etc/logrotate.d/$pkgname

	install -m 755 -D "$srcdir"/occ ./usr/bin/occ

	# Clean some unnecessary files.
	find . -name .gitignore -delete \
		-o -name .bower.json -delete \
		-o -name 'README*' -delete \
		-o -name 'CHANGELOG*' -delete \
		-o -name 'CONTRIBUTING*' -delete
	find . -name .github -type d -prune -exec rm -r {} \;
}

doc() {
	default_doc

	local target="$subpkgdir"/usr/share/webapps/$pkgname/core/doc
	mkdir -p "${target%/*}"
	ln -s ../../../doc/$pkgname/core "$target"
}

initscript() {
	pkgdesc="Init script that runs Nextcloud with php-fpm"
	depends="$pkgname $_php-fpm"

	local confdir="$subpkgdir/etc/$_php/php-fpm.d"
	local fpm_name="php-fpm${_php#php}"

	install -m 644 -D "$srcdir"/fpm-pool.conf "$confdir"/$pkgname.conf
	install -m 644 -D "$srcdir"/$pkgname.confd "$subpkgdir"/etc/conf.d/$pkgname
	install -m 755 -D "$srcdir"/$pkgname.cron "$subpkgdir"/etc/periodic/15min/$pkgname

	mkdir -p "$subpkgdir"/etc/init.d
	ln -s $fpm_name "$subpkgdir"/etc/init.d/$pkgname

	install -m 700 -o nextcloud -d "$subpkgdir"/var/tmp/$pkgname
}

pgsql() {
	pkgdesc="Nextcloud PostgreSQL support"
	depends="$pkgname $_php-pgsql $_php-pdo_pgsql"
	mkdir -p "$subpkgdir"
}

sqlite() {
	pkgdesc="Nextcloud SQLite support"
	depends="$pkgname $_php-sqlite3 $_php-pdo_sqlite"
	mkdir -p "$subpkgdir"
}

mysql() {
	pkgdesc="Nextcloud MySQL support"
	depends="$pkgname $_php-pdo_mysql"
	mkdir -p "$subpkgdir"
}

_default_apps() {
	pkgdesc="Nextcloud default apps"
	depends="$pkgname"

	local path; for path in "$pkgdir"/$_appsdir/*; do
		if grep -q '<default_enable\s*/>' "$path"/appinfo/info.xml; then
			depends="$depends $pkgname-${path##*/}"
		fi
	done

	mkdir -p "$subpkgdir"
}

_package_app() {
	local appname="${subpkgname#$pkgname-}"
	local appinfo="$pkgdir/$_appsdir/$appname/appinfo/info.xml"

	local name=$(xmlstarlet sel -t -v 'info/name/text()' "$appinfo")
	pkgdesc="Nextcloud ${name:-$appname} app"

	local php_deps=$(xmlstarlet sel -t -v 'info/dependencies/lib/text()' "$appinfo" \
		| xargs -r -n1 printf "$_php-%s\n")
	local app_deps=""

	case "$appname" in
		files_sharing)	app_deps="-federatedfilesharing";;
		photos)
				app_deps="-viewer"
				provides="$pkgname-gallery=$pkgver-r$pkgrel"
		;;

		# XXX: Provides/replaces for backward compatibility with <12.0.0-r2.
		files_pdfviewer | files_videoplayer)
			provides="$pkgname-${appname#files_}"
			replaces="$provides"
		;;
		user_ldap)
			provides="$pkgname-ldap"
			replaces="$provides"
		;;
	esac

	depends="$pkgname $php_deps ${app_deps//-/$pkgname-}"

	mkdir -p "$subpkgdir"/$_appsdir
	mv "$pkgdir"/$_appsdir/$appname "$subpkgdir"/$_appsdir/
}

sha512sums="6e199e136d93172add6a469fdeaacd7d43b7da9fe6b73e27c1baa44b2f0a9ea57caf4af271b977f35cdfcf5f15d811a75948c997bfa4e0cb7ce1a913166ec58d  nextcloud-19.0.1.zip
b7c80ad84e8f39ff99307130c9b081fa5b4d670359b9b4a03152ee677fa12389dacc311ec94ad27626461c21ed977d403fcc03c328a3d07b2e8f91ba5a4cbf9d  nextcloud19-dont-chmod.patch
56b187b5410452c5375b0503cfc154f86b8662f9590638c6975457675848b4251a8b1ed530741d815adfb4494ca44acc0ffea10471296be73026dcb4279aca60  nextcloud17-app-encryption-info-add-mcrypt.patch
aef3c92497d738d6968e0f0b0d415b4953500db24ae14af41ef972665cf7eff00cb6c53dc953845fdbb389c3c965a75b8b14b9247513c05cf4130fe1cfc61731  dont-update-htaccess.patch
d2100a837fef1eeae5f706650ab4c985d9e00f61efa5526ef76c7c1f5811c3906eb6c3c13c151eff9677a0c303faab64411a5a84d6792728bc520d2c618d7d5b  disable-integrity-check-as-default.patch
3fc3e06580a619d81b12f448976ffac34f0bb80fc73e9443fa213a73f160ba4b9bd14a26c134258ee12c04d8e103b46f4de10d7b11e4544a328878e57d436055  iconv-ascii-translit-not-supported.patch
df1a16414a278c205876ec86c210a02a9009954e2d4f9033ff3c9b76c371e2764ef3587db5a4b8f76302655c6c8688c8729d1685279a77d279d3839cc359fbcd  use-external-docs-if-local-not-avail.patch
5f73cd9399fa484ef15bd47e803c93381deffbc7699eceadbb5c27e43b20156806d74e5021a64d28f0165ef87b519e962780651711a37bceb9f0b04455dfdce1  nextcloud-config.php
7388458a9e8b7afd3d3269718306410ffa59c3c23da4bef367a4d7f6d2570136fae9dd421b19c1441e7ffb15a5405e18bb5da67b1a15f9f45e8b98d3fda532ba  nextcloud.logrotate
dcc57735d7d4af4a7ebbdd1186d301e51d2ae4675022aea6bf1111222dfa188a3a490ebd6e7c8a7ac30046cb7d93f81cec72a51acbc60d0c10b7fb64630c637a  nextcloud.confd
edb699ea6127b231793254115b334006c2d50a0d2ecc846188c3521ddffc3c0e19c5e2944f03cae81e6c645c859258380691081b1c522a22d40939b31db36e8a  nextcloud.cron
ee9073a6df4286cba2d1d855cf40863968f20677729b2c7848ab50a70d4915b8e84c957a850a03a707231256c11312e5792e7817dd50afbf73efe767fef2112d  fpm-pool.conf
959852e34f010e635470829d66713f3e22c47717ec2c6487759eed2b6aeff9fd1421fe0271d494a02781bd1c98beb2823583623ee2cf03057cd5db794627d6c2  occ"
